﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Apartments;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace blackhouse.BlackApart.Web {
	public class WebApartmentSortModel : ApartmentSortModel {

		#region Fields

		private SortDirection direction;
		private string sortBy;

		#endregion

		#region Properties

		public override SortDirection Direction {
			get {
				if (direction == 0) {
					HttpCookie fCookie = HttpContext.Current.Request.Cookies["sort_direction"];
					if (fCookie == null || !Enum.TryParse<SortDirection>(fCookie.Value, true, out this.direction)) {
						this.Direction = SortDirection.Asc;
						return this.Direction;
					}
				}
				return this.direction;
			}
			set {
				if (value != this.direction) {
					this.direction = value;
					HttpCookie fCookie = HttpContext.Current.Response.Cookies["sort_direction"];
					if (fCookie == null) {
						fCookie = new HttpCookie("sort_direction", this.direction.ToString());
						HttpContext.Current.Response.AppendCookie(fCookie);
					}
					else {
						fCookie.Value = this.direction.ToString();
						HttpContext.Current.Response.SetCookie(fCookie);
					}

				}
			}
		}

		public override string SortBy {
			get {
				if (string.IsNullOrEmpty(this.sortBy)) {
					HttpCookie fCookie = HttpContext.Current.Request.Cookies["sort_by"];
					if (fCookie == null || string.IsNullOrEmpty(fCookie.Value)) {
						this.SortBy = this.sorters.First().Key;
						return this.SortBy;
					}
					else this.sortBy = fCookie.Value;
				}
				return this.sortBy;
			}
			set {
				if (value != this.sortBy) {
					this.sortBy = value;
					HttpCookie fCookie = HttpContext.Current.Response.Cookies["sort_by"];
					if (fCookie == null) {
						fCookie = new HttpCookie("sort_by", this.sortBy);
						HttpContext.Current.Response.Cookies.Add(fCookie);
					}
					else
						fCookie.Value = this.sortBy;

				}
			}
		}


		#endregion

		#region Methods

		public void PopulateRepeater(Repeater repeater) {
			repeater.DataSource = this.sorters.Values;
			repeater.DataBind();
		}

		public void RepeaterCommand(object dataModel, WebControl controlToBindNav) {
			Sorter s = dataModel as Sorter;

			if (controlToBindNav != null && s != null) {
				SortDirection d = s.DefaultSort;
				bool active = s.Key == this.SortBy;

				if (active)
					d = (Direction == SortDirection.Asc) ? SortDirection.Desc : SortDirection.Asc;

				controlToBindNav.Attributes.Add("sort_by", s.Key);
				controlToBindNav.Attributes.Add("sort_direction", d.ToString().ToLower());
				controlToBindNav.Attributes.Add("sort", s.Key + ' ' + d.ToString().ToLower());

				string cssClass = "sort-pm sort-" + s.Key;
				if (active)
					cssClass += " sort-active-" + Direction.ToString().ToLower();
				if (!string.IsNullOrEmpty(controlToBindNav.CssClass))
					cssClass = controlToBindNav.CssClass + ' ' + cssClass;

				controlToBindNav.CssClass = cssClass;

				if (controlToBindNav is HyperLink) {
					HyperLink link = controlToBindNav as HyperLink;
					link.Text = s.Name;

				}
				if (controlToBindNav is ITextControl)
					(controlToBindNav as ITextControl).Text = s.Name;


			}
		}

		#endregion

	}
}